﻿module Lhz.ServiceCenter {
    export class Invoker {
        private webServiceUrl: string = "/service/WebServiceApi.asmx";
        private webServiceInvokeByInvokeArgumentUrl: string = this.webServiceUrl + "/InvokeByInvokeArgument";
        private webServiceInvokeByInvokeArgumentsUrl: string = this.webServiceUrl + "/InvokeByInvokeArguments";
        private webServiceGetGuidUrl: string = this.webServiceUrl + "/GetGuid";
        private cj = new Json.CycleJson();

        InvokeByInvokeArgument<TResult>(invokeArguments: Lhz.Interface.ServiceCenter.InvokeArguments<TResult>) {
            var sci = this;
            invokeArguments = $.extend({
                arguments: []
            }, invokeArguments);
            var wsa: Lhz.Interface.ServiceCenter.WebServiceArguments = {
                ApplicationNo: invokeArguments.applicationNo ? invokeArguments.applicationNo : "Sc",
                ServiceNo: invokeArguments.serviceNo,
                OperationNo: invokeArguments.operationNo,
                Arguments: []
            };
            for (var i = 0; i < invokeArguments.arguments.length; i++) {
                wsa.Arguments.push(JSON.stringify(this.cj.decycle(invokeArguments.arguments[i])));
            }
            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: this.webServiceInvokeByInvokeArgumentUrl,
                data: JSON.stringify({ invokeArgument: wsa }),
                dataType: "json",
                async: invokeArguments.async,
                success: function (response) {
                    var jsonInvokeResult: Lhz.Interface.ServiceCenter.Result<string> = JSON.parse(response.d);
                    var invokeResult: Lhz.Interface.ServiceCenter.Result<TResult> = {
                        ApplicationNo: jsonInvokeResult.ApplicationNo,
                        ServiceNo: jsonInvokeResult.ServiceNo,
                        OperationNo: jsonInvokeResult.OperationNo,
                        Arguments: [],
                        Result: JSON.parse(jsonInvokeResult.Result)
                    };
                    for (var argument of jsonInvokeResult.Arguments) {
                        argument = JSON.parse(argument);
                        sci.cj.retrocycle(argument);
                        invokeResult.Arguments.push(argument);
                    }
                    sci.cj.retrocycle(invokeResult.Result);
                    if (invokeArguments.successCallback) {
                        invokeArguments.successCallback(invokeResult, invokeArguments.data);
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(JSON.parse(jqXHR.responseText).Message);
                }
            });
        }

        InvokeByInvokeArguments<TResult>(invokeArgumentsList: Lhz.Interface.ServiceCenter.InvokeArgumentsList) {
            var sci = this;
            invokeArgumentsList = $.extend({
                argumentsList: [],
                async: true
            }, invokeArgumentsList);
            if (invokeArgumentsList.serviceNos.length != invokeArgumentsList.operationNos.length || invokeArgumentsList.serviceNos.length != invokeArgumentsList.argumentsList.length) {
                throw "多服务调用错误，参数的服务编号、操作编号和参数集合的个数不匹配。";
            }
            var wsas: Lhz.Interface.ServiceCenter.WebServiceArguments[] = [];
            for (var i = 0; i < invokeArgumentsList.serviceNos.length; i++) {
                var wsa: Lhz.Interface.ServiceCenter.WebServiceArguments = {
                    ApplicationNo: (invokeArgumentsList.applicationNos && invokeArgumentsList.applicationNos[i]) ? invokeArgumentsList.applicationNos[i] : "Sc",
                    ServiceNo: invokeArgumentsList.serviceNos[i],
                    OperationNo: invokeArgumentsList.operationNos[i],
                    Arguments: []
                };
                for (var argument in invokeArgumentsList.argumentsList[i]) {
                    wsa.Arguments.push(JSON.stringify(this.cj.decycle(argument)));
                }
                wsas.push(wsa);
            }
            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: this.webServiceInvokeByInvokeArgumentUrl,
                data: JSON.stringify({ invokeArgument: wsas }),
                dataType: "json",
                async: invokeArgumentsList.async,
                success: function (response) {
                    var jsonInvokeResults: Lhz.Interface.ServiceCenter.Result<string>[] = JSON.parse(response.d);
                    var invokeResults: Lhz.Interface.ServiceCenter.Result<TResult>[] = [];
                    for (var jsonInvokeResult in jsonInvokeResults) {
                        var invokeResult: Lhz.Interface.ServiceCenter.Result<TResult> = {
                            ApplicationNo: jsonInvokeResult.ApplicationNo,
                            ServiceNo: jsonInvokeResult.ServiceNo,
                            OperationNo: jsonInvokeResult.OperationNo,
                            Arguments: [],
                            Result: JSON.parse(jsonInvokeResult.Result)
                        };
                        for (var argument of jsonInvokeResult.Arguments) {
                            argument = JSON.parse(argument);
                            sci.cj.retrocycle(argument);
                            invokeResult.Arguments.push(argument);
                        }
                        sci.cj.retrocycle(invokeResult.Result);
                        invokeResults.push(invokeResult);
                    }
                    if (invokeArgumentsList.successCallback) {
                        invokeArgumentsList.successCallback(invokeResults, invokeArgumentsList.data);
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(JSON.parse(jqXHR.responseText).Message);
                }
            });
        }

        getGuid(): string {
            var guid;
            $.ajax({
                type: "POST",
                contentType: "application/json",
                async: false,
                url: this.webServiceGetGuidUrl,
                success: function (result) {
                    guid = result.d;
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                }
            });
            return guid;
        }
    }
}
